<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息管理系统</title>
 </head>
<body>
    <?php
        include_once "HeaderNav.html";  //引入头部
    ?>
    <h2>学生信息</h2>
    <?php
        //1.使用mysqli对象建立数据库连接
        require "Conn.php";
        if(isset($_GET['page'])){
            $page=$_GET['page'];
        }else{
            $page = 1; //页码
        }
        $pagesize = 6; //每页显示记录数
        $p = ($page-1)*$pagesize;
        //统计数据库表总记录数
        $sql1 = "SELECT count(*) FROM student";
        $result1=$db->query($sql1);
        $count = $result1->fetch_row()[0];
        $pagecount=ceil($count/$pagesize); //向上取整
        // echo $pagecount;
        // exit();
        //2.执行SQL语句
        $sql = "select * from student limit $p,$pagesize";
        $result = $db->query($sql);  //执行查询,返回的是一个资源类型的结果集
        //3.处理结果集
        //$rows = $result->fetch_all();     //(1)获取所有的结果，并以数组方式返回
        //$rows = $result->fetch_array();   //(2)获取一行结果，并以数组方式返回(关联，索引)
        //$rows = $result->fetch_assoc();   //(3)获取一行结果，并以数组方式返回(关联)
        //$rows = $result->fetch_row();     //(4)获取一行结果，并以数组方式返回(索引)
        if($result){
            echo "<table class='tb'>";
            echo "<tr><th>学号</th><th>姓名</th><th>班级号</th><th>性别</th><th>手机</th><th>邮箱</th><th>操作</th></tr>";
            while($rows = $result->fetch_assoc()){
                echo "<tr>";
                echo "<td>".$rows["StudentID"]."</td>";
                echo "<td>".$rows["StudentName"]."</td>";
                echo "<td>".$rows["ClassNo"]."</td>";
                echo "<td>".$rows["Sex"]."</td>";
                echo "<td>".$rows["Mobile"]."</td>";
                echo "<td>".$rows["Email"]."</td>";
                echo "<td> <a href='Details.php?StudentID=".$rows["StudentID"]."'>详细</a> ".
                          " <a href='Update.php?StudentID=".$rows["StudentID"]."'>编辑</a> ".
                          " <a href='Delete.php?StudentID=".$rows["StudentID"]."' onclick='return confirm(\"你确定需要删除吗？\")'>删除</a> ".
                     "</td>";
                echo "</tr>";
            }
            echo "</table>";
        }
        $result->close();
        //4.断开连接
        $db->close();
    ?>
    <table class="tb" style="margin-top:10px">
        <tr style="height:20px">
            <td><a href="Students.php?page=1">首页</a></td>
            <td><a href="Students.php?page=<?php echo $page==1?1:($page-1)?>">上一页</a></td>
            <td><a href="Students.php?page=<?php echo $page==$pagecount?$pagecount:($page+1) ?>">下一页</a></td>
            <td><a href="Students.php?page=<?php echo $pagecount ?>">尾页</a></td>
            <?php
                for($i=1;$i<=$pagecount;$i++){
                    echo "<td><a href='Students.php?page=$i'> $i </a></td>";
                }
            ?>
        </tr>
    </table>
    <?php
        include_once "Footer.html";  //引入脚部
    ?>
</body>
</html>